home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Trading on the Edge
/
Trading On The Edge - CD-ROM Toolkit (Wayzata Technology)(2031)(1994).bin
/
pc
/
mac_file
/
vendor_d
/
ga_softw
/
ooga
/
how-to.lis
< prev
next >
Wrap
File List
|
1991-02-03
|
11KB
|
389 lines
;;; -*- Mode:Lisp; Package:OOGA; Base:10; Syntax:COMMON-LISP -*-
#||
RESTRICTED RIGHTS LEGEND
Use, duplication, or disclosure by the Government is subject to
restrictions as set forth in subdivision (b)(3)(ii) of the Rights in
Technical Data and Computer Software Clause at 52.227-7013 of the DOD
FAR Supplement.
TSP (The Software Partnership)
P.O. Box 991
Melrose, MA 02176
Copyright 1990 by Lawrence Davis and Daniel Cerys, all rights reserved.
||#
(in-package :ooga)
#|
This file contains routines for using the traditional genetic algorithm
to optimize a function of your own.
|#
;************************************************************
; EXAMPLE 1: USING THE TRADITIONAL GENETIC ALGORITHM
;;; The following expressions create a genetic
;;; algorithm that maximizes the sum of the chromosome bits.
;;; The optimal chromosome will consist entirely of ones.
;;; STEP 1
;;; Describe the one vector genetic algorithm
(defclass ONE-VECTOR-GENETIC-ALGORITHM (traditional-ga) ())
;;; STEP 3
;;; Describe the sum vector evaluator
(defclass SUM-VECTOR-EVALUATOR (evaluator) ())
;;; Describe the sum vector evaluation function. It sums the
;;; chromosome fields.
(defmethod EVALUATE-MEMBER
((evaluator sum-vector-evaluator)
(population-member population-member))
(apply '+ (chromosome population-member)))
;;; STEP 5
;;; Describe the particulars of this algorithm
(def-append-method GET-PARTICULARS ((ga one-vector-genetic-algorithm))
`((evaluator ,(make-instance 'sum-vector-evaluator))
(population-size 50)
(desired-trials 1600)
(bit-string-length 30)))
;;; use (trial-run 'one-vector-genetic-algorithm) to run this
;;; GA.
;************************************************************
; EXAMPLE 2: USING THE TRADITIONAL GENETIC ALGORITHM
;;; The following expressions create a genetic
;;; algorithm that minimizes the sum of the chromosome bits.
;;; The optimal chromosome will consist entirely of zeros.
;;; Because we are minimizing, we should define a new class of
;;; population member and a new evaluation-better-p method.
;;; STEP 1
;;; Describe zero vector genetic algorithm
(defclass ZERO-VECTOR-GENETIC-ALGORITHM (traditional-ga) ())
;;; STEP 2
;;; Describe zero vector population member class
(defclass ZERO-VECTOR-POPULATION-MEMBER (population-member) ())
;;; Minimize instead of maximizing
(defmethod EVALUATION-BETTER-P ((member1 zero-vector-population-member)
(member2 zero-vector-population-member))
"Non-default behavior: lower evaluation values are better."
(< (evaluation member1) (evaluation member2)))
;;; STEP 3
;;; Describe zero vector evaluator
(defclass ZERO-VECTOR-EVALUATOR (evaluator) ())
;;; Describe zero vector evaluation function. It sums the
;;; chromosome fields. (This is the same method as the
;;; evaluator above.)
(defmethod EVALUATE-MEMBER
((evaluator zero-vector-evaluator) zero-vector-population-member)
(apply '+ (chromosome zero-vector-population-member)))
;;; STEP 5
;;; Tell OOGA the zero vector ga particulars
(defmethod GET-PARTICULARS append
((ga zero-vector-genetic-algorithm))
`((population-size 50)
(desired-trials 1600)
(bit-string-length 30)
(evaluator ,(make-instance 'zero-vector-evaluator))
(population-member-class zero-vector-population-member))
)
;;; use (trial-run 'zero-vector-genetic-algorithm) to run this
;;; GA.
;************************************************************
; EXAMPLE 3: USING THE STEADY-STATE GENETIC ALGORITHM
;;; The following expression creates a steady-state genetic algorithm
;;; that minimizes the function used above, much faster from the point
;;; of view of total evaluations. This way of doing it by using
;;; a component class requires much less code on the user's part.
;;; Note that the order of the component classes here is
;;; important. If the order were reversed, the steady-state
;;; methods would not replace the traditional ga methods.
;;; STEP 1
(defclass STEADY-STATE-ZERO-VECTOR-GA
(steady-state-ga
zero-vector-genetic-algorithm)())
;;; use (trial-run 'steady-state-zero-vector-ga) to run this
;;; GA.
;************************************************************
; EXAMPLE 4: USING THE REAL-VALUED GENETIC ALGORITHM
;;; The following expressions create a real-valued genetic
;;; functionally equivalent to GA 5-1, except that now we use
;;; the real-valued-genetic-algorithm as a component class.
;;; We alter the parameters of the operators by altering
;;; the probability values, mutation, and creep specs. These
;;; changes are for illustration purposes only; they degrade the
;;; performance of the genetic algorithm.
;;; The real-value-f6 evaluator has already been defined.
;;; STEP 1
;;; Describe this GA.
(defclass REAL-VALUED-F6-GA
(advanced-techniques-genetic-algorithm
real-value-genetic-algorithm) ())
;;; Describe the particulars. This function is created by
;;; copying the corresponding function in the definition of the
;;; real-value-genetic-algorithm, modifying the parameter
;;; values, and adding the evaluator, parent selection
;;; technique, and fitness technique particulars to finish the
;;; description of the GA, and then specifying the other
;;; particulars that complete the GA.
;;; STEP 5
(def-append-method GET-PARTICULARS ((ga real-valued-f6-ga))
;;;THESE PARTICULARS ARE COPIED FROM THE
;;;REAL-VALUE-GENETIC-ALGORITHM DEFINITION, BUT PARAMETER
;;;VALUES ARE MODIFIED.
`((representation-technique
,(make-instance 'real-number-representation
:real-number-specs '((0 4194303 t))
:chromosome-length 2))
(initialization-technique
,(make-instance 'random-real-number-initialization))
(operator-list
,(list (make-instance 'uniform-list-crossover)
(make-instance 'average-real-crossover)
(make-instance 'real-number-mutation
:mutation-rate .6
:mutation-specs '((0 4194303 t)))
(make-instance 'real-number-creep
:creep-rate .7
:creep-specs '((100000 t)))
(make-instance 'real-number-creep
:creep-rate .5
:creep-specs '((5000 t)))))
(operator-weights ,(list 10 40 10 30 10))
(reproduction-parameterization-techniques
,(list (make-instance
'interpolate-operator-weights
:interpolation-specs
'((10 40 10 30 10) (10 20 0 40 30)))))
;;;THESE PARTICULARS ARE SPECIFIC TO THIS GA
(evaluator ,(make-instance 'real-number-f6))
(population-size 100)
(desired-trials 4000)))
;;; use (trial-run 'real-valued-f6-ga) to run this
;;; GA.
;************************************************************
; EXAMPLE 5: USING THE ORDER-BASED GENETIC ALGORITHM
;;; The following expressions create an order-based genetic
;;; algorithm that attempts to evolve a sorting of the elements
;;; of a 50-member list.
;;; STEP 1
;;; Define the genetic algorithm
(defclass SORTING-GENETIC-ALGORITHM
(advanced-techniques-genetic-algorithm
order-based-genetic-algorithm) ())
;;; STEP 2
;;; We are minimizing.
(defclass SORTING-POPULATION-MEMBER (population-member)())
(defmethod EVALUATION-BETTER-P
((member1 sorting-population-member)
(member2 sorting-population-member))
(< (evaluation member1) (evaluation member2)))
;;; Describe the display method for this type of population member
(defmethod DISPLAY-MEMBER
((representation-technique permuted-list)
(population-member sorting-population-member))
(format *standard-output* "~%")
(format *standard-output* " ~a" (evaluation population-member))
(loop for node in (firstn 30 (chromosome population-member))
do (format *standard-output* " ~a" node))
)
;;; STEP 3
;;;The evaluator
(defclass SORTING-EVALUATOR (evaluator) ())
(defmethod EVALUATE-MEMBER
((evaluator sorting-evaluator)
(member sorting-population-member))
"Promote a sorted list"
(loop for n from 1
for value in (chromosome member)
summing (abs (- n value))))
(defmethod LIST-TO-PERMUTE ((evaluator sorting-evaluator))
(loop for n from 1 to 50 collect n))
;;; STEP 5
(def-append-method GET-PARTICULARS ((ga sorting-genetic-algorithm))
`((population-size 100)
(desired-trials 6000)
(population-member-class sorting-population-member)
(evaluator ,(make-instance 'sorting-evaluator))))
;;; use (trial-run 'sorting-genetic-algorithm) to run this
;;; GA.
;************************************************************
; EXAMPLE 6: USING THE ADAPTIVE TECHNIQUES
;;;
;;; The following expressions derive operator weights for the
;;; genetic algorithm just described. The process of creating
;;; an adaptive genetic algorithm is more complicated than that
;;; of creating the previous types of genetic algorithms. Read
;;; the documentation carefully.
;;; STEP 1
;;; Define the genetic algorithm. Note that the population and
;;; reproduction modules are installed here rather than with the
;;; install-particulars process. These modules should be in
;;; place before the install-particulars process begins, so that
;;; they are available to receive the particular techniques. If
;;; they were installed as part of the particulars list for the
;;; adaptive-sorting-genetic-algorithm, they would be installed
;;; last, and the previously-installed particulars would be
;;; lost.
(defclass ADAPTIVE-SORTING-GENETIC-ALGORITHM
(sorting-genetic-algorithm
adapt-initial-operator-weights) ()
(:default-initargs
:population-module (make-instance
'adaptive-sorting-population-module)
:reproduction-module (make-instance
'adaptive-sorting-reproduction-module)))
;;; Define the population module. It must be installed before
;;; the particulars are installed. It must have some new
;;; component classes in order to carry out adaptation
;;; correctly.
(defclass ADAPTIVE-SORTING-POPULATION-MODULE
(trace-operator-weights
adaptive-operator-module
basic-population-module)
())
;;; Define the reproduction module. It must have the
;;; adaptive-reproduction-module class as a component.
(defclass ADAPTIVE-SORTING-REPRODUCTION-MODULE
(adaptive-reproduction-module) ())
(def-append-method GET-PARTICULARS ((ga adaptive-sorting-genetic-algorithm))
`((population-member-class adaptive-sorting-population-member)
(reproduction-parameterization-techniques nil)
(initial-operator-weights (70 30))))
;;; STEP 2
;;; Define the population member. It must have the
;;; adaptation-population-member class as a component.
(defclass ADAPTIVE-SORTING-POPULATION-MEMBER
(sorting-population-member
adaptation-population-member)
())
;;; Now the algorithm is ready to run. To have it find initial
;;; operator weights, run the function
;;; (find-initial-operator-weights
;;; (make-instance 'adaptive-sorting-genetic-algorithm))
;;; With the new initialization algorithm, the weights found
;;; tend to lie near (80 20), rather than the default value of
;;; (70 30).
;;; To see the algorithm adapt weights, run the function
;;; (trial-run 'adaptive-sorting-genetic-algorithm).